//
//  SettingsViewController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/3/17.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "SettingsViewController.h"
#import "PersonView.h"
#import "feedBackViewController.h"
#import "OrderedActivityController.h"
#import "OrderedTeacherController.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
#import "UserInfoModel.h"
#import "FirstClassifyViewController.h"
#import "OrderingTeacherController.h"
#import "UserModel.h"
#import "ActivityFinishedController.h"

@interface SettingsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain)UserModel *model;

@end

@implementation SettingsViewController{
    UITableView* _tableView;
    NSString*    _cacheStr;         //缓存
    PersonView*  _personHeader;     //headerView
    UISwitch*    _switchBtn;        //消息推送开关
}

#pragma mark - 初始化方法
-(instancetype)init{
    if (self==[super init]) {
        // 设置标题&TabBar图标
        self.title = @"我";
        self.tabBarItem.image = [UIImage imageNamed:@"settings_tabBar"];
        self.hidesBottomBarWhenPushed = false;
    }
    return self;
}

#pragma mark - 系统回调方法
- (void)viewWillAppear:(BOOL)animated {
    // 每次从新出现界面,就获取数据
    // 网络请求个人资料
    [self updateUserInfo];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我";
    self.view.backgroundColor = [UIColor yellowColor];
    
    
    _tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.delegate=self;
    _tableView.dataSource=self;


    // 取出UserInfo模型
    self.model = [UserInfoModel shareInstance].userModel;
    //设置tableView__headerView
    UIView* headerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 150)];//背景图层View
    _personHeader=[[PersonView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 110)];
    if (self.model != nil) {
        _personHeader.iconStr = self.model.picture_url;
        _personHeader.majorStr = self.model.major;
        _personHeader.nameStr = self.model.name;
        [_personHeader setupView];
    }
    [headerView addSubview:_personHeader];
    _tableView.tableHeaderView=headerView;
    [self.view addSubview:_tableView];


}

#pragma mark - 网络请求数据
-(void)updateUserInfo {
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/getUser"
                                        parameters:@{@"s_id": [UserInfoModel shareInstance].stu_id, @"token": [UserInfoModel shareInstance].token}
                                           success:^(NSDictionary *respondDictionary) {
                                               self.model = [[UserModel alloc] initWithDictionary:respondDictionary[@"User"]];
                                               [_tableView reloadData];

                                               [UserInfoModel shareInstance].userModel = self.model;
                                               [[UserInfoModel shareInstance] saveToFile];

                                               _personHeader.iconStr = self.model.picture_url;
                                               _personHeader.majorStr = self.model.major;
                                               _personHeader.nameStr = self.model.name;
                                               [_personHeader setupView];
                                           } failure:^(NSError *error) {
                
                [SVProgressHUD showfailed];
            }];
}

#pragma mark--tableViewCell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"id"];
    }
    
    //为cell设置内容
    if(indexPath.section==0){
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        if(indexPath.row==0){
            cell.textLabel.text=@"正在参与的预约活动";
            [cell.detailTextLabel setText:[NSString stringWithFormat:@"%d",self.model?self.model.ai_num:0]];
        }else if (indexPath.row==1){
            cell.textLabel.text=@"未获得学分的预约活动";
            [cell.detailTextLabel setText:[NSString stringWithFormat:@"%d",self.model?self.model.act_num:0]];
        }else if (indexPath.row==2){
            cell.textLabel.text=@"学分统计";
        }

    }else if (indexPath.section==1){
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        if(indexPath.row == 0) {
            cell.textLabel.text=@"正在预约的导师";
            //此处应由数据传入
            [cell.detailTextLabel setText:[NSString stringWithFormat:@"%d",self.model?self.model.ti_num:0]];
        }else if(indexPath.row == 1) {
            cell.textLabel.text=@"预约过的导师";
            //此处应由数据传入
            [cell.detailTextLabel setText:[NSString stringWithFormat:@"%d",self.model?self.model.tea_num:0]];
        }
    }else if (indexPath.section==2){
        if(indexPath.row==0){
            cell.textLabel.text=@"缓存清理";
            //此处应由数据传入
            CGFloat cacheSize = [SDImageCache sharedImageCache].getDiskCount/1000.0/1000.0;
            _cacheStr=[NSString stringWithFormat:@"%.2fM",cacheSize];
            [cell.detailTextLabel setText:_cacheStr];
        }else{
            cell.textLabel.text=@"消息推送";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            //设置ON/OFF Switch
            _switchBtn=[[UISwitch alloc] init];
            _switchBtn.on=YES;
            [_switchBtn addTarget:self action:@selector(messagePushTapped) forControlEvents:UIControlEventValueChanged];
            cell.accessoryView = _switchBtn;
            
        
        }
    }
    else{
        //设置退出登录
        UILabel* label=[[UILabel alloc] init];
        [label setText:@"退出登录"];
        label.frame=CGRectMake(self.view.width/2-40, 0, 80, 40);
        [cell.contentView addSubview:label];
    
    }
    return cell;
}

//划分4个单元
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

//划分每个单元的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){ // 正在参与的预约活动 + 已完成但未获得学分的活动 +学分统计
        return 3;
    }else if (section==1){ // 预约过的导师 + 正在预约的导师
        return 2;
    }
    else if (section==2){ // 缓存清理 + 消息推送
        return 2;
    }
    else{ // 退出登录
        return 1;
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"活动相关";
    }else if(section == 1){
        return @"导师相关";
    }else if(section == 2){
        return @"系统设置";
    }else {
        return @"";
    }
}


// 消息推送开关动作
-(void) messagePushTapped{
    #pragma mark--消息推送开关动作
    _switchBtn.on=!_switchBtn.on;
    NSLog(@"messagePushTapped");
}

//选中动作
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //设置tableViewCell选中风格
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    
    if (indexPath.section==0) {
        if(indexPath.row==0){//正在参与的预约活动
            OrderedActivityController *activity = [[OrderedActivityController alloc] init];
            [self.navigationController pushViewController:activity animated:true];
        }else if (indexPath.row==1){//已完成但未获得学分的活动
            ActivityFinishedController *controller = [[ActivityFinishedController alloc] init];
            [self.navigationController pushViewController:controller animated:true];
        }else if (indexPath.row==2){//已完成的预约活动
            FirstClassifyViewController *controller = [[FirstClassifyViewController alloc] init];
            [self.navigationController pushViewController:controller animated:true];
        }
    }else if (indexPath.section==1){
        if(indexPath.row==0){// 正在预约的导师
            OrderingTeacherController *ordering = [[OrderingTeacherController alloc] init];
            [self.navigationController pushViewController:ordering animated:true];
        }else if(indexPath.row==1){// 预约过的导师
            OrderedTeacherController *ordered = [[OrderedTeacherController alloc] init];
            [self.navigationController pushViewController:ordered animated:true];
        }
    }else if (indexPath.section==2){
        if(indexPath.row==0){//缓存清理
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定清除缓存?" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            UIAlertAction *done = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
            }];
            [alert addAction:cancel];
            [alert addAction:done];
            [self presentViewController:alert animated:true completion:nil];
        }
    }
//    else if (indexPath.section==2){
//        if(indexPath.row==0){//反馈
//            feedBackViewController* feedBackView=[feedBackViewController new];
//            [self.navigationController pushViewController:feedBackView animated:YES];
//        }
//        else if (indexPath.row==1){//尚德书院
//            UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"当前版本" message:@"当前版本1.0.0" preferredStyle:UIAlertControllerStyleAlert];
//            
//            UIAlertAction *done1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
//            [alert1 addAction:done1];
//            [self presentViewController:alert1 animated:true completion:nil];
//        }
//        
//    }
    else{

        // 删除登录信息
        [[UserInfoModel shareInstance] logout];

        // 跳转登录界面
        ((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController = [[LoginViewController alloc] init];
        
    }
    
}

@end
